package com.example.pingze.repository;

import com.example.pingze.dto.PoemAndPoetInfoDto;
import com.example.pingze.entity.PoemInfo;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

public interface PoemInfoRepository extends JpaRepository<PoemInfo, Integer>, JpaSpecificationExecutor<PoemInfo> {

    List<PoemInfo> findPoemInfoByWriter(String writer);
    PoemInfo findPoemInfoByDynasty(String value);
    @Query("select "+
            "new com.example.pingze.dto.PoemAndPoetInfoDto("+
            "pm.id,pm.writer,pm.dynasty,pt.simpleIntro,pm.title," +
            "pm.types,pm.content,pm.remark,pm.translations," +
            "pm.appreciation)"+
            "from PoemInfo pm "+
            "join PoetInfo pt "+
            "on pm.writer = pt.poetName "+
            "where pm.id = :id")
    PoemAndPoetInfoDto findAllById(String id);

    PoemInfo findPoemInfoById(String poemId);

    @Query("select distinct u.writer from PoemInfo u")
    List<String> findWriter();

    List<PoemInfo> findPoemInfoByTitleContains(String title);

    PoemInfo findPoemInfoByWriterAndTitleContains(String writer, String title);
}
